using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class FPSWeaponEquipment : FPSItemEquipment
{
	private CharacterSystem character;

	private FPSController fpsController;

	private float timeTemp;

	private AudioSource audioSource;

	private Animator animator;

	[Header("Ammo")]
	public ItemData ItemUsed;

	public bool InfinityAmmo;

	public int ClipSize = 30;

	public byte BulletNum = 1;

	public int Ammo = 30;

	public int AmmoMax = 30;

	[HideInInspector]
	public int AmmoHave;

	[Header("Firing")]
	public float AnimationSpeed = 1f;

	public bool OnAnimationEvent;

	public int UsingType;

	public bool HoldFire = true;

	public float FireRate = 0.09f;

	public byte Spread = 20;

	public byte Damage = 10;

	public float Force = 10f;

	public Vector2 KickPower = Vector2.zero;

	public Vector3 AimPosition = new Vector3(-0.082f, 0.06f, 0f);

	public byte MaxPenetrate = 1;

	public float Distance = 100f;

	[Header("Sound / FX")]
	public AudioClip SoundFire;

	public AudioClip SoundReload;

	public AudioClip SoundReloadComplete;

	public AudioClip[] DamageSound;

	public GameObject MuzzleFX;

	public Transform MuzzlePoint;

	public GameObject ProjectileFX;

	public Transform Pointer;

	[Header("Other")]
	public float panicfire;

	public float PanicFireMult = 0.1f;

	public float FOVZoom = 65f;

	public float SpreadZoomMult = 1f;

	public bool HideWhenZoom;

	private float animationSpeedTemp = 1f;

	private bool reloading;

	private float spreadmult;

	private void Start()
	{
		reloading = false;
		animator = GetComponent<Animator>();
		audioSource = GetComponent<AudioSource>();
		if ((bool)base.transform.root)
		{
			character = base.transform.root.GetComponent<CharacterSystem>();
			fpsController = base.transform.root.GetComponent<FPSController>();
			if (character == null)
			{
				character = base.transform.root.GetComponentInChildren<CharacterSystem>();
			}
			if (fpsController == null)
			{
				fpsController = base.transform.root.GetComponentInChildren<FPSController>();
			}
		}
		else
		{
			character = base.transform.GetComponent<CharacterSystem>();
			fpsController = base.transform.GetComponent<FPSController>();
		}
		if ((bool)character)
		{
			character.DamageSound = DamageSound;
			if ((bool)character.inventory)
			{
				Pointer = character.inventory.Pointer;
			}
			character.PrimaryWeaponDistance = Distance;
			character.gameObject.SendMessage("OnEquipChanged", UsingType, SendMessageOptions.DontRequireReceiver);
		}
		if ((bool)fpsController)
		{
			fpsController.zooming = false;
		}
		Hide(visible: true);
		timeTemp = Time.time;
		if ((bool)animator)
		{
			animationSpeedTemp = animator.speed;
		}
	}

	public override void Trigger()
	{
		if (HoldFire || !OnFire1)
		{
			if ((bool)character && Time.time > timeTemp + FireRate)
			{
				Shoot();
				timeTemp = Time.time;
			}
			base.Trigger();
		}
	}

	public override void OnTriggerRelease()
	{
		base.OnTriggerRelease();
	}

	public override void Trigger2()
	{
		if (fpsController != null)
		{
			fpsController.Zoom(FOVZoom, AimPosition);
		}
		base.Trigger2();
	}

	public override void OnTrigger2Release()
	{
		base.OnTrigger2Release();
	}

	public void Shoot()
	{
		if (Ammo > 0 || InfinityAmmo)
		{
			if (animator != null && animator.isActiveAndEnabled)
			{
				animator.speed = AnimationSpeed;
				animator.SetTrigger("shoot");
			}
			if (!OnAnimationEvent)
			{
				OnAction();
			}
			if (character != null)
			{
				character.AttackAnimation(UsingType);
			}
		}
	}

	public override bool Reload()
	{
		if (Ammo >= AmmoMax || (Ammo <= 0 && AmmoHave <= 0))
		{
			return false;
		}
		if (!reloading && (bool)audioSource && (bool)SoundReload)
		{
			audioSource.PlayOneShot(SoundReload);
		}
		if (ItemUsed != null)
		{
			int itemNum = character.inventory.GetItemNum(ItemUsed);
			if (!InfinityAmmo && character != null && character.inventory != null && itemNum <= 0)
			{
				return false;
			}
		}
		if ((bool)animator)
		{
			animator.SetTrigger("reloading");
		}
		reloading = true;
		base.Reload();
		return true;
	}

	public override void ReloadComplete()
	{
		int num = ClipSize;
		if (AmmoMax - Ammo < num)
		{
			num = AmmoMax - Ammo;
		}
		if (character != null && character.inventory != null && ItemUsed != null)
		{
			if (AmmoHave <= 0)
			{
				reloading = false;
				return;
			}
			if (AmmoHave < num)
			{
				num = AmmoHave;
			}
			character.inventory.RemoveItem(ItemUsed, num);
		}
		Ammo += num;
		if (Ammo >= AmmoMax)
		{
			reloading = false;
		}
		else
		{
			Reload();
		}
		if ((bool)audioSource && (bool)SoundReloadComplete)
		{
			audioSource.PlayOneShot(SoundReloadComplete);
		}
		base.ReloadComplete();
	}

	private void Update()
	{
		if (!reloading && Ammo <= 0)
		{
			Reload();
		}
		Hide(visible: true);
		spreadmult = 1f;
		if ((bool)fpsController)
		{
			fpsController.PlayerView.FPSCamera.hideWhenScoping = HideWhenZoom;
			if (HideWhenZoom && fpsController.zooming)
			{
				Hide(visible: false);
				spreadmult = SpreadZoomMult;
			}
		}
		if (panicfire < 0.01f)
		{
			panicfire = 0f;
		}
		panicfire += (0f - panicfire) * 5f * Time.deltaTime;
		if (IsVisible)
		{
			if ((bool)animator)
			{
				animator.SetInteger("shoot_type", UsingType);
			}
			if (character != null && character.inventory != null && ItemUsed != null)
			{
				AmmoHave = character.inventory.GetItemNum(ItemUsed);
			}
			if (CollectorSlot != null)
			{
				CollectorSlot.NumTag = Ammo;
			}
			if (!InfinityAmmo)
			{
				Info = Ammo + "/" + AmmoHave;
			}
			else
			{
				Info = "Infinity";
			}
		}
	}

	public void SetCollectorSlot(ItemCollector collector)
	{
		CollectorSlot = collector;
		if (collector.NumTag != -1)
		{
			Ammo = collector.NumTag;
		}
	}

	public override void OnAction()
	{
		if ((bool)animator)
		{
			animator.speed = animationSpeedTemp;
		}
		if (Ammo > 0 || InfinityAmmo)
		{
			if (!InfinityAmmo)
			{
				Ammo--;
			}
			if (BulletNum <= 0)
			{
				BulletNum = 1;
			}
			byte b = (byte)((float)(int)Spread * spreadmult);
			byte seed = (byte)Random.Range(0f, 255f * spreadmult);
			if (!MuzzlePoint)
			{
				MuzzlePoint = base.transform;
			}
			if (IsVisible)
			{
				if ((bool)MuzzleFX)
				{
					GameObject gameObject = Object.Instantiate(MuzzleFX, MuzzlePoint.position, MuzzlePoint.rotation);
					gameObject.transform.parent = base.transform;
					UnityEngine.Object.Destroy(gameObject, 2f);
				}
				if ((bool)audioSource && (bool)SoundFire && IsVisible && audioSource.enabled)
				{
					audioSource.PlayOneShot(SoundFire);
				}
				for (int i = 0; i < BulletNum; i++)
				{
					if ((bool)Pointer)
					{
						Vector3 vector = Pointer.forward + new Vector3((float)Random.Range(-b, b) * 0.001f, (float)Random.Range(-b, b) * 0.001f, (float)Random.Range(-b, b) * 0.001f) * spreadmult;
						if ((bool)ProjectileFX && (bool)fpsController)
						{
							Object.Instantiate(ProjectileFX, Pointer.position + vector * 5f, Quaternion.LookRotation(vector));
						}
					}
				}
			}
			if ((bool)fpsController)
			{
				fpsController.Kick(KickPower);
			}
			if (character != null && (bool)Pointer)
			{
				if (panicfire < 0.5f && BulletNum == 1)
				{
					b = 0;
				}
				character.DoDamageByItemIndex(Pointer.position, Pointer.forward, ItemIndex, seed);
			}
			panicfire += 5f;
		}
		base.OnAction();
	}
}
